# 1, 操作
# 业务1：登录->用户分组—>添加分组->删除分组
# 业务2：登录->客服->客服话术->添加话术->删除话术
# 业务3：登录->商品->商品分类->添加分类->删除分类
# 2、使用pytest框架做，要有setup和teardown
import pytest,requests
from faker import Faker
import uuid
uuid1 = str(uuid.uuid4()).split('-')[0]
faker = Faker(locale='zh_cn')
class TestCRMEB:
    project_url = "http://60.205.190.66:8899"
    @pytest.fixture(scope='function',autouse=True)
    def setup_teardown(self):
        response = requests.post(self.project_url + "/adminapi/login",json = {"account":"admin","pwd":"920509","imgcode":"aaa"})
        self.token = response.json()['data']['token']
        yield
        print("测试用例执行完毕")
    def test_usergroup(self):
        # 添加
        name = faker.name()
        response = requests.post(self.project_url + "/adminapi/user/user_group/save.html",json = {"group_name":str(name)},
                      headers={"Authori-Zation":"Bearer "+str(self.token)})
        assert response.json()['msg'] == "提交成功！"
        assert response.status_code ==200

        #查询
        response1 = requests.get(self.project_url + "/adminapi/user/user_group/list?page=1&limit=15",headers={"Authori-Zation":"Bearer "+str(self.token)})
        assert response1.status_code ==200
        for i in response1.json()['data']['list']:
            if i['group_name'] == str(name):
                self.id = i['id']

        # 删除
        response2 = requests.delete(self.project_url + "/adminapi/user/user_group/del/" + str(self.id),headers={"Authori-Zation":"Bearer "+str(self.token)})
        assert response2.status_code == 200
        assert response2.json()['msg'] == "删除成功!"
    def test_Customer(self):
        # 创建

        response1 = requests.post(self.project_url + "/adminapi/app/wechat/speechcraft", json = {"cate_id":"","title":"赵四舞蹈的玄妙1",
                                                                                                 "message":str(uuid1),"sort":0},headers={"Authori-Zation":"Bearer "+str(self.token)})
        assert response1.status_code == 200
        assert response1.json()['msg'] == "创建话术成功"

        # 查询
        response2 = requests.get(self.project_url + "/adminapi/app/wechat/speechcraft?page=1&limit=15&cate_id=",headers={"Authori-Zation":"Bearer "+str(self.token)})
        assert response2.status_code == 200
        for i in response2.json()['data']['list']:
            if i['message'] == str(uuid1):
                self.id = i['id']

        # 编辑
        response3 = requests.put(self.project_url + "/adminapi/app/wechat/speechcraft/" + str(self.id),headers={"Authori-Zation":"Bearer "+str(self.token)},json={"cate_id":0,"title":"赵四舞蹈的玄妙1","message":"小天才","sort":0})
        assert response3.status_code == 200
        assert response3.json()['msg'] == "修改成功"

        # 删除
        response4 = requests.delete(self.project_url + "/adminapi/app/wechat/speechcraft/" + str(self.id),headers={"Authori-Zation":"Bearer "+str(self.token)})
        assert response4.status_code == 200
        assert response4.json()['msg'] == "删除成功"

    def test_CATEGORY(self):
        # 新增
        response1 = requests.post(self.project_url+"/adminapi/product/category.html",headers={"Authori-Zation":"Bearer "+str(self.token)},json={"pid":14,"cate_name":"小天才给大家表演" + str(uuid1),"pic":"","big_pic":"","sort":0,"is_show":1})
        assert response1.status_code == 200
        assert response1.json()['msg'] == '添加分类成功!'

        #查询 ?pid=&is_show=&page=1&cate_name=
        response2 = requests.get(self.project_url + "/adminapi/product/category",params={"page":1,"cate_name":"小天才给大家表演" + str(uuid1)},headers={"Authori-Zation":"Bearer "+str(self.token)})
        print(response2.json())
        assert response2.status_code == 200
        for i in response2.json()['data']['list'][0]['children']:
            if i['cate_name'] == "小天才给大家表演" + str(uuid1):
                self.id = i['id']

        # 删除
        response3 = requests.delete(self.project_url + "/adminapi/product/category/" + str(self.id),headers={"Authori-Zation":"Bearer "+str(self.token)})
        assert response3.json()['msg'] == "删除成功!"
        assert response3.status_code == 200




if __name__ == '__main__':
    pytest.main()